$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'watersub/vdataerrsub/list',
		postData:{'key': vm.q.key,'value':vm.q.value,'dept':vm.q.dept},
        datatype: "json",
        colModel: [			
			{ label: '设备号', name: 'deviceId', index: 'device_id', width: 200, key: true,hidden:true },
			{ label: '数据日期', name: 'nowDate', index: 'now_date', width: 130 },
			{ label: '表卡编号', name: 'clientId', index: 'client_id', width: 120 },
			{ label: '客户名', name: 'clientName', index: 'client_name', width: 120 },
			{ label: '设备地址', name: 'deviceAddress', index: 'device_address', width: 120 },
			{ label: 'nowData', name: 'nowData', index: 'now_data', width: 80,hidden:true },
			{ label: '状态', name: 'deviceStatus', index: 'device_status', width: 100,hidden:false },
			{ label: '客户地址', name: 'clientAddress', index: 'client_address', width: 250 },
			{ label: 'statusCode', name: 'statusCode', index: 'status_code', width: 80,hidden:true },
			{ label: 'agoData', name: 'agoData', index: 'ago_data', width: 80,hidden:true },
			{ label: 'agoDeviceStatus', name: 'agoDeviceStatus', index: 'ago_device_status', width: 80,hidden:true },
			{ label: '小区', name: 'proName', index: 'pro_name', width: 100,hidden:false },
			{ label: '设备数量', name: 'deviceCount', index: 'device_count', width: 100,hidden:false },
			{ label: '竣工时间', name: 'installerName', index: 'installer_name', width: 100 },
			{ label: '说明', name: 'installerPhone', index: 'installer_phone', width: 80 },
			{ label: '调试员', name: 'debugerName', index: 'debuger_name', width: 100 },
			{ label: '调试员电话', name: 'debugerPhone', index: 'debuger_phone', width: 120 },
			{ label: 'masterNum', name: 'masterNum', index: 'master_num', width: 80,hidden:true },
			{ label: 'slaveNum', name: 'slaveNum', index: 'slave_num', width: 80,hidden:true },
			{ label: 'cardNum', name: 'cardNum', index: 'card_num', width: 80,hidden:true },
			{ label: 'deviceArea', name: 'deviceArea', index: 'device_area', width: 80,hidden:true },
			{ label: 'deviceNei', name: 'deviceNei', index: 'device_nei', width: 80,hidden:true },
			{ label: 'deviceBuild', name: 'deviceBuild', index: 'device_build', width: 80,hidden:true },
			{ label: 'deviceUnit', name: 'deviceUnit', index: 'device_unit', width: 80,hidden:true },
			{ label: 'routeNum', name: 'routeNum', index: 'route_num', width: 80,hidden:true },
			{ label: 'meterNum', name: 'meterNum', index: 'meter_num', width: 80,hidden:true },
			{ label: 'deviceAddressCode', name: 'deviceAddressCode', index: 'device_address_code', width: 80,hidden:true },
			{ label: 'clientSex', name: 'clientSex', index: 'client_sex', width: 80,hidden:true },
			{ label: 'clientAge', name: 'clientAge', index: 'client_age', width: 80,hidden:true },
			{ label: 'clientIdCard', name: 'clientIdCard', index: 'client_id_card', width: 80,hidden:true },
			{ label: 'buildNum', name: 'buildNum', index: 'build_num', width: 80,hidden:true },
			{ label: 'unitNum', name: 'unitNum', index: 'unit_num', width: 80,hidden:true },
			{ label: 'houseNum', name: 'houseNum', index: 'house_num', width: 80,hidden:true },
			{ label: 'cityName', name: 'cityName', index: 'city_name', width: 80,hidden:true },
			{ label: 'areaName', name: 'areaName', index: 'area_name', width: 80,hidden:true },
			{ label: 'cityId', name: 'cityId', index: 'city_id', width: 80,hidden:true },
			{ label: 'areaId', name: 'areaId', index: 'area_id', width: 80,hidden:true },
			{ label: 'unitIdG', name: 'unitIdG', index: 'unit_id_g', width: 80,hidden:true },
			{ label: '管理所', name: 'unitNameG', index: 'unit_name_g', width: 80,hidden:true },
			{ label: 'unitIdW', name: 'unitIdW', index: 'unit_id_w', width: 80,hidden:true },
			{ label: '分公司', name: 'unitNameW', index: 'unit_name_w', width: 100,hidden:false },
			{ label: 'delFlag', name: 'delFlag', index: 'del_flag', width: 80,hidden:true },

			{ label: 'typeCode', name: 'typeCode', index: 'type_code', width: 80,hidden:true },
			{ label: '类型', name: 'typeName', index: 'type_name', width: 100 },
			{ label: '模组', name: 'modeName', index: 'mode_name', width: 80,hidden:true },
			{ label: 'abnormal', name: 'abnormal', index: 'abnormal', width: 80,hidden:true },
			{ label: 'deptId', name: 'deptId', index: 'dept_id', width: 80,hidden:true }
        ],
		viewrecords: true,
        height: $(window).height()-165,
        rowNum: 16,
		rowList : [16,30,50],
        rownumbers: true, 
        rownumWidth: 60,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
			$(window).on('resize', function() {
				var $content = $('.content');
				$content.height($(this).height() - 154);
				$content.find('iframe').each(function() {
					$(this).height($content.height());
				});

				$("#deptTreeLeft").css({height:($("#content > div.col-md-10").height()-45)+'px'});
			}).resize();
        }
    });

	$(function () {
		$('#inputClear').click(function () {
			$('input')[0].value = "";
			vm.q.value="";
		})
	});
});



function zTreeOnDblClick(event, treeId, treeNode) {
	vm.q.dept = treeNode.deptId;
	vm.newpage = true;
	vm.reload();
	//alert(treeNode.deptId);
	//alert(treeNode ? treeNode.tId + ", " + treeNode.name : "isRoot");
};

var settingLeft = {
	data: {
		simpleData: {
			enable: true,
			idKey: "deptId",
			pIdKey: "parentId",
			rootPId: -1
		},
		key: {
			url:"nourl"
		}
	},
	callback: {
		onDblClick: zTreeOnDblClick
	}
};

var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		vdataerr: {},
		tdataerr:{},
		newpage : false,
		searchKeyDictInfo: {},
		q:{
			key:null,
			value:null,
			dept:-1
		}
	},
	methods: {
		getSearchKeyDict: function(){
			$.get(baseURL + "sys/dict/list",{name:"qkey_vdataerr"}, function(r){
				vm.searchKeyDictInfo = r.page.list;
			});
		},
		query: function () {
			if(vm.q.value != null && vm.q.value != ""
				&& (vm.q.key == null || vm.q.key == "")){
				alert('请选择查询条件!');
				return;
			}
			vm.newpage=true;
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.vdataerr = {};
		},
		detail: function (event) {
			var deviceId = getSelectedRow();
			if(deviceId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "详情";
            
            vm.getInfo(deviceId)
		},
		getDeptTreeLeft: function(){
			$.get(baseURL + "water/waterdept/queryDeptErrSubWater", function(r){
				//console.log(r);
				ztree = $.fn.zTree.init($("#deptTreeLeft"), settingLeft, r);
				var node = ztree.getNodeByParam("deptId", vm.q.dept);
				if(node != null){
					ztree.selectNode(node);
					vm.q.dept = node.deptId;
				}
				ztree.expandAll(true);
			})
		},
		getInfo: function(deviceId){
			$.get(baseURL + "watersub/vdataerrsub/info/"+deviceId, function(r){
                vm.vdataerr = r.vdataerr;
            });
		},
		getStatus: function(deviceId){
			/*$.get(baseURL + "watersub/tdataerrsub/status", function(r){
				vm.tdataerr = r;
			});*/
		},
		outxls: function(){
			var total = $("#jqGrid").jqGrid('getGridParam', 'records') + "";
			var dept = vm.q.dept==null ? "" : vm.q.dept+  "";
			var url = baseURL
				+ "watersub/vdataerrsub/export?params="
				+ encodeURIComponent(JSON.stringify({key:vm.q.key,value:vm.q.value,dept:dept,limit:total,page:"1"}));

			//console.log(decodeURIComponent(url));
			location.href = url;
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			if(vm.newpage){page = "1";}
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'key': vm.q.key,'value':vm.q.value,'dept':vm.q.dept},
                page:page
            }).trigger("reloadGrid");
		}
	},
	created: function (){
		this.getDeptTreeLeft();
		this.getStatus();
		this.getSearchKeyDict();
	}
});